package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.QaAnswerBean;
import cn.bocweb.spinning.model.Bean.QaappDetailBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.AnswerGvImgAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.ContainsEmojiEditText;
import de.greenrobot.event.EventBus;

public class WriteAnswerActivity extends BaseActivity {

    String id;
    int replySum;
    AnswerGvImgAdapter mAnswerGvImgAdapter;
    List<String> photos;
    UserPresenter mPresenter;
    UserBean.DataEntity dataEntity;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.answer_score)
    TextView mAnswerScore;
    @Bind(R.id.answer_content)
    TextView mAnswerContent;

    @Bind(R.id.answer_time)
    TextView mAnswerTime;
    @Bind(R.id.answer_count)
    TextView mAnswerCount;
    @Bind(R.id.answer_edit)
    ContainsEmojiEditText mAnswerEdit;
    @BindColor(R.color.white)
    int white;
    @Bind(R.id.gv_img)
    RecyclerView mGvImg;
    @Bind(R.id.answer_submit)
    Button mAnswerSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_answer);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        dataEntity = new UserBean.DataEntity();
        mPresenter = new UserPresenterImp();
        photos = new ArrayList<>();
        mAnswerGvImgAdapter = new AnswerGvImgAdapter(photos);

        initImg();
        initIntent();
        initToolbar();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    public void onEventMainThread(QaappDetailBean bean) {
        if (bean.getReturn_code() == 0) {
            initView(bean.getData());
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }

    public void onEventMainThread(QaAnswerBean bean) {
        if (bean.getReturn_code() == -201) {
            DeviceUtil.MyToast(this, "请先登录");
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
        if (bean.getReturn_code() == 0) {
//            Intent intent = new Intent();
//            QaappDetailBean.ReplyBean replyBean = new QaappDetailBean.ReplyBean();
//            replyBean.setContent(mAnswerContent.getText().toString());
//            replyBean.setExpert();
//            intent.putExtra(Constant.QUES_ANSWER_DETAIL, )
            setResult(
                    Constant.QUES_DETAIL_BACK_QUES_LIST,
                    new Intent().putExtra(
                            "isAcivity",
                            true));
            finish();
        }
    }

    public void onEventMainThread(ErrorBean bean) {
        DeviceUtil.MyToast(this, bean.getErrorMsg());
    }

    private void initImg() {
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        mGvImg.setLayoutManager(manager);
        mGvImg.setAdapter(mAnswerGvImgAdapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        id = intent.getStringExtra(Constant.TO_QUES_DETAIL_ANSWER);
        replySum = intent.getIntExtra(Constant.TO_QUES_DETAIL_ANSWER_NUM, 0);

        mPresenter.qaDetail(id, 1);
    }

    private void initView(QaappDetailBean.DataBean bean) {
        toolbar.setTitle(new StringBuffer(bean.getNickname()).append("的问题").toString());

        mAnswerTime.setText(
                DeviceUtil.getTimeByTwoTimeStampForPhp(
                        Long.parseLong(bean.getTimeline()) * 1000,
                        System.currentTimeMillis()));
        mAnswerContent.setText(bean.getContent());
        mAnswerCount.setText(String.valueOf(replySum));
        mAnswerScore.setText(bean.getScore());
        if (bean.getPhoto() != null) {
            photos.addAll(bean.getPhoto());
        }
        mAnswerGvImgAdapter.notifyDataSetChanged();

        initToolbar();
    }

    private void initToolbar() {
        toolbar.setTitleTextColor(white);
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.answer_submit)
    public void onClick() {
        dataEntity.readInfoFromLocal(this);
        if (TextUtils.isEmpty(dataEntity.getUserid())) {
            Intent intent = new Intent(this, LoginAcivity.class);
            startActivity(intent);
        } else {
            mPresenter.qaAnswer(dataEntity.getUserid(), id, mAnswerEdit.getText().toString());
        }
    }


}
